var express = require("express");
var router = express.Router();
let crypto = require("crypto");
let jwt = require("jsonwebtoken");
let userServive = require("../services/userService");

/* GET users listing. */
router.get("/", function (req, res, next) {
  res.send("respond with a resource");
});
router.get("/list", async (req, res, next) => {
  let result = await userServive.userQuery({});
  res.json({
    code: 200,
    msg: "欢迎使用用户的接口",
    data: result,
  });
});
router.get("/studentlist", (req, res, next) => {
  let token = req.headers.token;
  jwt.verify(token, "2306", async (err, info) => {
    if (!err && info.usertype == 1) {
      //没有错 并且是老师
      let result = await userServive.userQuery({
        type: 2,
      }); //查询
      res.json({
        code: 200,
        msg: "查询成功",
        data: result,
      });
    } else {
      res.json({
        code: 203,
        msg: "token异常或者权限不够",
      });
    }
  });
});

router.post("/addmany", async (req, res, next) => {
  let { list } = req.body; //获取数据
  let usernameList = list.map((item) => item["姓名"]);
  let result = await userServive.userQuery({
    username: { $in: usernameList },
  });
  if (result.length) {
    //有存在的用户名
    res.json({
      code: 203,
      msg: "有存在的用户名",
      data: result.map((item) => item.username),
    });
  } else {
    let md5 = crypto.createHash("md5"); //md5  base64  sha1
    let userpwd = md5.update("123456").digest("hex");
    let  infoList  = usernameList.map(username=>{
      return  {
        username,userpwd,type:2
      }
    });
    let result2 = await  userServive.userAddMany(infoList);
    //批量新增
    res.json({
      code:200,
      msg:"新增成功",
      data:result2
    })



  }
});
router.post("/addstudent", (req, res, next) => {
  let token = req.headers.token;
  let { username, userpwd, type } = req.body;
  jwt.verify(token, "2306", async (err, info) => {
    if (err) {
      res.json({
        err,
        code: 203,
        msg: "token异常请重新登录",
      });
    } else {
      //没有问题
      if (info.usertype == 1) {
        //身份OK   可以判断
        //检查用户名是否存在
        let result1 = await userServive.userQuery({
          username,
        });
        if (result1.length) {
          res.json({
            code: 203,
            msg: "该用户名已经存在",
          });
        } else {
          let md5 = crypto.createHash("md5"); //md5  base64  sha1
          userpwd = md5.update(userpwd).digest("hex");
          let result2 = await userServive.userAdd({
            username,
            userpwd,
            type,
          });
          res.json({
            code: 200,
            msg: "新增成功",
            data: result2,
          });
        }
      } else {
        res.json({
          code: 203,
          msg: "用户的权限不够",
        });
      }
    }
  });
});

router.post("/login", async (req, res, next) => {
  let { username, userpwd } = req.body;

  let md5 = crypto.createHash("md5"); //md5  base64  sha1
  userpwd = md5.update(userpwd).digest("hex");

  let result = await userServive.userQuery({ username, userpwd });
  if (result.length) {
    //能查到就有用户名和密码匹配
    let info = result[0]; //查询到信息
    let token = jwt.sign(
      {
        userid: info._id,
        usertype: info.type,
        username: info.username,
      },
      "2306",
      {
        expiresIn: 1000 * 60,
      }
    );
    let studentRouter = [
      {
        path: "addleave",
        com: "addleave",
        msg: "新增请假",
      },
    ];
    let teacherRouter = [
      { path: "student", com: "student", msg: "学生管理" },
      { path: "leave", com: "leave", msg: "请假管理" },
      {
        path:"dataanalysis",com:"dataanalysis",msg:"数据分析"
      }
    ];
    res.json({
      code: 200,
      msg: "登录成功",
      token,
      routerList: info.type == 1 ? teacherRouter : studentRouter,
    });
  } else {
    res.json({
      code: 201,
      msg: "登录失败",
      data: "用户名和密码不匹配",
    });
  }
});
//产生默认的老师
router.post("/add", async (req, res, next) => {
  let { username, userpwd } = req.body;

  let md5 = crypto.createHash("md5"); //md5  base64  sha1
  userpwd = md5.update(userpwd).digest("hex");
  //加密
  let result = await userServive.userAdd({
    username,
    userpwd,
    type: 1,
  });
  res.json({
    code: 200,
    msg: "新增",
    data: result,
  });
});

module.exports = router;
